/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.testsvg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.example.app.R;

/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年4月20日 下午3:54:05
 * @Title <pre>
 * todo
 * </pre>
 * @Description <pre>
 * todo
 * </pre>
 */

@SuppressLint("JavascriptInterface")
public class TestAct extends Activity {
	WebView webView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tstesvg);

		webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.addJavascriptInterface(new WebAppInterface(), "Android");
		String svg = loadSvg();
		webView.loadData(svg, "image/svg+xml", "utf-8");
	}

	String loadSvg() {
		try {
			BufferedReader input = new BufferedReader(new InputStreamReader(
					getAssets().open("test.svg")));
			StringBuffer buf = new StringBuffer();
			String s = null;
			while ((s = input.readLine()) != null) {
				buf.append(s);
				buf.append('\n');
			}
			input.close();
			return buf.toString();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	public class WebAppInterface {
	    /** Show a toast from svg */
		@JavascriptInterface
	    public void showToast(String toast) {
	      Toast.makeText(TestAct.this, toast, Toast.LENGTH_SHORT).show();
	    }
	  }
}
